const { query } = require('../config/db');

// async function getAllData() {
//   try {

//     // const weekRes = await getWeekView()

//   } catch (error) {

//   }
// }

// 网站数据
async function getWeekView() {
  const sql = `select * from viewsOfTheWeek where week=0`;
  return await query(sql);
}
async function getPageView() {
  const sql = 'select * from pageView';
  return await query(sql);
}
async function getUserRes() {
  const sql = 'select * from userRegistrations where week=0';
  return await query(sql);
}
async function getPostNum() {
  const sql = 'select * from postNum';
  return await query(sql);
}

// 板块数据
async function getAllSections() {
  const sql = 'select * from allSections';
  return await query(sql);
}
// 删除板块数据
async function delectSection(sid) {
  console.log(sid);
  const sql = `delete from allSections where sid = '${sid}';`;
  return await query(sql);
}
// 增加板块数据
async function addSection(sid, name, detail, isInset) {
  if (isInset) {
    const sql = `insert into allSections (name,detail) values ('${name}','${detail}');`;
    return await query(sql);
  }
  const sql = `update allSections set name = '${name}', detail = '${detail}' where sid = '${sid}';`;
  return await query(sql);
}

// 所有用户数据
async function getAllUser() {
  const sql = 'select * from users';
  return await query(sql);
}

// 修改用户数据
async function updataInfoByAdmin(
  email,
  password,
  phone,
  role,
  uid,
  username
) {
  const sql = `update users set username='${username}', role='${role}', email = '${email}', password='${password}',phone='${phone}' where uid = '${uid}'`;
  return query(sql);
}

// 删除用户数据
async function deleteUser(uid) {
  const sql = `delete from users where uid = '${uid}';`;
  return query(sql);
}

// 板块帖子数据
async function getAllPost() {
  const sql = `select * from post;`;
  return query(sql);
}
// 删除帖子数据
async function goToRecycle(sid, pid) {
  const sql = `delete from post where sid = '${sid}' and pid = '${pid}';`;
  return query(sql);
}
// 修改帖子数据
async function updatePost(name, intro, story, portrait, pid) {
  const sql = `update post set name='${name}', intro = '${intro}',story = '${story}',portrait='${portrait}' where pid = '${pid}'`;
  return query(sql);
}
// 增加帖子数据
async function addPost(name, intro, story, portrait, sid) {
  const sql = `insert into post (name, intro, story, portrait, sid) values ('${name}','${intro}','${story}','${portrait}','${sid}');`;
  return await query(sql);
}


module.exports = {
  getWeekView,
  getPageView,
  getUserRes,
  getPostNum,
  getAllSections,
  getAllUser,
  updataInfoByAdmin,
  deleteUser,
  getAllPost,
  goToRecycle,
  updatePost,
  addPost,
  delectSection,
  addSection,
}